/* 
 *Yii常规操作ajax扩展库
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *@author martin
 */
;(function($){
	$.fn.settings = {
		loadingClass : 'loading',
		page_static : 'page_static',
		page_asc : 'asc',
		page_desc : 'desc',
		check_error : '不可为空!请填写!',
		check_error_class : 'post_check_error',
		check_pass_class : 'post_check_pass'
	}
        
        $.fn.getArray = function(id){
                var data = new Array();
                $('#'+id).each(function(i){
                    data[i] = $(this).attr('name')+'='+$(this).val();
                });
                return  data.join('&');
        }
	
	$.fn.AjaxPost = function(id,url,data){
		var option = $.extend({
			type:'POST',
			url:url,
			data:data,
			success:function(msg){
				$.fn.ajaxUpdate(id,$.fn.getUrl(id));
				alert(msg);
			}
		},option||{});
		$.ajax(option);
	}
	
	$.fn.ajaxGet = function(id,url,callBackUrl){
		var option = $.extend({
			type:'GET',
			url:url,
			success:function(msg){
				$.fn.ajaxUpdate(id,callBackUrl);
				alert(msg);
			}
		},option||{});
		$.ajax(option);
	}
	
	$.fn.checkNull = function(id){
		var doSubmit = true;
		$('#'+id+' input').each(function(){
			if($(this).val()==''||$(this).val()==$.fn.settings.check_error){
				$(this).addClass($.fn.settings.check_error_class);
				$(this).val($.fn.settings.check_error);
				$(this).click(function(){
					$(this).removeClass($.fn.settings.check_error_class);
					$(this).val('');
				});
				doSubmit = false;
			}
			else{
				$(this).addClass($.fn.settings.check_pass_class);
			}
		});
		return doSubmit;
	}
	
	$.fn.setDialog = {
		//set dialog callback 
		/*
		yes : function(){
		  alert( e.target );
		  return false;
		},*/
	}
	
	$.fn.loading = function(set){
		if(set == 'yes'){
                    $('<div id="ajaxbox-loading"><div></div></div>').appendTo('body');
		}else if(set == 'no'){
                    $("#ajaxbox-loading").remove();
		}
	}
	
	$.fn.openDialog = function(header,content,yes,no){
		var settings = $.fn.setDialog;
		easyDialog.open({
		  container : {
			header : header,
			content : content,
			yesFn : settings.yes ? settings.yes : false,
			noFn : settings.no ? settings.no : (settings.loading ? false : true),
			yesText: yes,
			noText : no,
			overlay : true
		  },
		  overlay:settings.oberlay
		});
	}
	
	$.fn.deletes = function(id,url){
		var option = $.extend({
			type:'GET',
			url:url,
			success:function(res){
				$.fn.ajaxUpdate(id,$.fn.getUrl(id));
			}
		},option || {});
		$.ajax(option);
	}
	
	$.fn.getUrl = function(id){
		var url = $('#'+id+' .selected a').attr('href');
		var num = $('#'+id+' .selected').text();
		var len = $.fn.getUrl.checkLen(id); 
		if(url == undefined){
			return window.location;
		}else if(1 < len){
			return url;
		}else{		
			return url.replace(/[2-9]/g,--num);
		}
	}
	
	$.fn.getUrl.checkLen = function(id){
		return len = $('#'+id).find('.rows').length;
	}
	
	$.fn.ajaxUpdate = function(id,url){
		//$('#'+id).addClass($.fn.settings.loadingClass);
		$.fn.loading('yes');
		var option = $.extend({
			type:'GET',
			url:url,
			success:function(data){
				$('#'+id).replaceWith($('#'+id,data)).serialize();
				$('#'+id).removeClass($.fn.settings.loadingClass);
				//$('.'+$.fn.settings.page_asc).removeClass($.fn.settings.page_static);
				$.fn.loading('no');
				$('.'+$.fn.settings.page_desc).removeClass($.fn.settings.page_static);
			}
		},option || {});
		$.ajax(option);
	}

        $.fn.checkImg = function(id,mun){
            var count = mun;
            var img='';
            var url = '/upload/getFile'
             if(0 < count){
                 var btnFn = function(){
                        $.getJSON(url, function(data){
                          $.each(data, function(i,item){
                              img = item.replace('albums', "thumbs");
                            $("<img/>").attr("src",'/'+img).appendTo('#'+id+'');
                            if ( i == count ) return false;
                          });
                        });
                    };
                    var delFn = function(){
                        $.get("/upload/delete");
                    }
                    easyDialog.open({
                      container : {
                        header : '系统消息',
                        content : '您有'+count+'张展示图片未使用,是否现在加载?选择是将加载到展示图片栏,并使用,选择否将这些未使用的图片删除!',
                        yesFn : btnFn,
                        noFn : delFn
                      }
                    });
             }
       }
})(jQuery);